package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.ClientTransfer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.ClientMapper;
import cn.wolfcode.crm.mapper.ClientTransferMapper;
import cn.wolfcode.crm.query.ClientTransferQueryObject;
import cn.wolfcode.crm.service.IClientTransferService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ClientTransferServiceImpl implements IClientTransferService {
    @Autowired
    private ClientTransferMapper mapper;
    @Autowired
    private ClientMapper clientMapper;


    public void deleteByPrimaryKey(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public int insert(ClientTransfer record) {
      return  mapper.insert(record);
    }

    public ClientTransfer selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<ClientTransfer> selectAll() {
        return mapper.selectAll();
    }

    public void updateByPrimaryKey(ClientTransfer record) {
        mapper.updateByPrimaryKey(record);
    }

    public PageResult query(ClientTransferQueryObject qo) {
        int count = mapper.queryForCount(qo);
        if (count == 0) {
            return new PageResult(0, null);
        }
        List<ClientTransfer> list = mapper.queryForList(qo);
        return new PageResult(count, list);
    }

    public void trunOverClient(ClientTransfer clientTransfer) {
        //接收到的 参数
        try {

            clientTransfer.setTransdate(new Date());
            //设置当前用户的id
            Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
            clientTransfer.setOperator(currentUser);
            //将clientTransfer保存
            int i = mapper.insert(clientTransfer);
            //成功之后还要修改客户的责任人
            //根据客户id查出客户信息
            Client client =new Client();
            client.setId(clientTransfer.getClient().getId());
            client.setPrincipal(clientTransfer.getNewprincipal());
            client.setInputuser(currentUser);
            client.setInputdate(new Date());
            clientMapper.upPrincipalAfterTrunOver(client);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
